package se.webbzon.boltzmann.battle.battleaction;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import se.webbzon.boltzmann.battle.group.BattleGroup;
import se.webbzon.boltzmann.battle.object.npc.BattleNPC;

public abstract class SingleHostileBattleActionFactory extends BattleActionFactory {

	/*============================================================
	Public Methods
	============================================================*/
	
	public SingleHostileBattleActionFactory() {};
	
	public SingleHostileBattleActionFactory(BattleActionFactoryIcon icon) {
		super(icon);
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	@Override public int targets(Collection<BattleGroup> all, BattleNPC caster) {
		int targets = 0;
		for (BattleGroup group : all) {
			if (group.getFactionId() != caster.getBattleGroup().getFactionId())
				targets += group.participants();
		}
		return targets;
	}

	@Override public List<BattleNPC> getTarget(	Collection<BattleGroup> all, 
												BattleNPC caster, int n) {
		ArrayList<BattleNPC> targets = new ArrayList<BattleNPC>();
		int target = n;
		
		// Retrieve the (n+1):th available character
		for (BattleGroup group : all) {
			if (group.getFactionId() != caster.getBattleGroup().getFactionId()) {
				final int size = group.participants();
				if (target > size-1)
					target -= size;
				else {
					targets.add(group.getParticipant(target));
					break;
				}
			}
		}

		return targets;
	}

}
